import logging
from flask_restplus import Resource, Namespace, fields, marshal_with
from common.utils.decorators import login_required
from flask import g, request, jsonify, Response
from app.models import db
from app import redis_clent, app
from flask_restplus import reqparse, inputs
from app.tasks.test import test
from app.routes.user.schemas import UserSchemas
from .marshmallow import (
    UserRegistrationQuerySerializer,
    UserRegistrationBodySerializer,
    UserInfoQuerySerializer,
    UserBodySerializer,
    UserDataSerizlizer,
    UserLoginQuerySerizlizer,
    UserTokenSerizlizer,
    UserListQuerySerializer,
    RoleBodyListSerializer,
)
from ...models.user import User, Role

LOG = logging.getLogger(__name__)

user_ns = Namespace("user")

user_mh = UserSchemas(user_ns)


class UserTokenView(Resource):
    @user_ns.expect(user_mh.user_login_query(), validate=True)
    @user_ns.marshal_with(user_mh.user_token())
    def post(self):
        data = UserLoginQuerySerizlizer().load(data=request.json)
        response = UserTokenSerizlizer().dump(data)

        return response


class UserPutDeleteView(Resource):
    # method_decorators = {"get": [login_required]} # 检查用户登录的装饰器

    @user_ns.marshal_with(user_mh.user_info_body())
    def get(self, id):

        try:
            user = User.query.get(id)
        except Exception as e:
            raise e

        response = UserBodySerializer().dump(user)

        return response

    def delete(self, id):

        try:
            user = User.query.get(id)
        except Exception as e:
            raise e

        user.deleted = True
        db.session.commit()

        return Response(status=204 if user else 500)

    @user_ns.expect(user_mh.user_registr_query(), validate=False)
    def put(self, id):

        try:
            user = User.query.filter(User.id == id).update(request.json)
        except Exception as e:
            raise e

        db.session.commit()

        return Response(status=202 if user else 500)


class UserLoginView(Resource):
    # method_decorators = {"get": [login_required]} # 检查用户登录的装饰器
    @user_ns.expect(user_mh.user_registr_query(), validate=False)
    @user_ns.marshal_with(user_mh.user_registr_body())
    def post(self):
        user = UserRegistrationQuerySerializer().load(data=request.json)
        response = UserRegistrationBodySerializer().dump(user)
        return response


class UserInfoView(Resource):
    # method_decorators = [login_required]

    @user_ns.doc(params=user_mh.user_list_query())
    @user_ns.marshal_with(user_mh.user_info_body())
    def get(self):

        user = UserInfoQuerySerializer().load(data={"id": g.user_id})
        response = UserBodySerializer().dump(user)

        return response


class UserListView(Resource):
    # method_decorators = [login_required]

    @user_ns.doc(params=user_mh.user_list_query())
    @user_ns.marshal_with(user_mh.user_list_body())
    def get(self):

        user, total = UserListQuerySerializer().load(data=request.args)
        response = UserDataSerizlizer().dump({"total": total, "data": user})

        return response


class UserRoleListView(Resource):
    # method_decorators = [login_required]

    @user_ns.marshal_with(user_mh.role_body())
    def get(self):

        roles = Role.query.filter().all()

        response = RoleBodyListSerializer(many=True).dump(roles)

        return response
